#include "gsm_sms.h"

SM_PARAM *initSM(char *phone,char* sms_center)
{
	int len;
	SM_PARAM *pSM;

	len = strlen(phone);

	if( len == 11 && (0 == memcmp(phone, "13", 2) || 0 == memcmp(phone,"15",2)) ){
		pSM = malloc(sizeof(SM_PARAM));
		strcpy(pSM->TPA, "86");
		strcat(pSM->TPA, phone);
	}
	else if( len == 13 && (0 ==memcmp(phone, "8613", 4) || 0 == memcmp(phone,"8615",4)) ){
		pSM = malloc(sizeof(SM_PARAM));
	}
	else{
		return (SM_PARAM *)0;
	}

	strcpy(pSM->SCA, sms_center/*"8613800571500"*/);	//Hangzhou center
	strcpy(pSM->TP_UD, "0A0B");			//Unicode of "AB"
	pSM->index = 4;
	pSM->TP_PID = 0x0;					//default type
	pSM->TP_DCS = 0x08;					//unicode

	return pSM;
}

void destroySM(SM_PARAM *pSM)
{
	free(pSM);
}

//UD only receive bytes stream of Unicode
void setSM_UD(SM_PARAM *pSM, char *src, int len)
{
	if( (SM_PARAM*)0 == pSM )
		return;
	if( len > UD_MAX_SIZE)
		len = UD_MAX_SIZE & 0xfffe;	//1 unicode = 2 bytes
	memcpy(pSM->TP_UD, src, len);
	pSM->index = len;
	pSM->TP_UD[len] = 0;			// "\0"
}
	
void readme()
{
	printf("****How to use****:\n");
	printf("SM_PARAM *pmsg;\n");
	printf("SM_PDU pdu;\n");
	printf("pmsg = initSM(\"13777436020\");\n");
	printf("setSM_UD(pmsg, UD_Stream, N_UDbytes);\n");
	printf("//@param2:unicode bytes stream\n");
	printf("//@param3:unicode bytes\n");
	printf("SM2PDU(pmsg, &pdu);\n");
	printf("destroySM(pmsg)\n");
	printf("toSeriel(pdu.CMGF);\n");
	printf("delay(10);\n");
	printf("toSeriel(pdu.CMGS);\n");
	printf("delay(10);\n");
	printf("toSeriel(pdu.PDU);\n");
	printf("delay(10);\n");
}
